package me.zhengjie.modules.rest;

import cn.hutool.core.date.DateTime;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.modules.domain.Api;
import me.zhengjie.modules.service.ApiService;
import me.zhengjie.modules.utils.Contents;
import me.zhengjie.modules.utils.ReturnObject;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.service.ApiInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class ApiController {

    private final ApiService apiService;
    //String name,String variableName,String url,String method,Integer cycle,boolean keyword,boolean page
    @PostMapping("/saveCreateApi")
    public @ResponseBody Object saveCreateApi(@RequestBody Api api){
        ReturnObject returnObject=new ReturnObject();
        System.out.println(api);
        Api api1=apiService.queryApiByVariableName(api.getVariableName());
        if(api1==null){
            api.setCreateTime(new DateTime());
            api.setDeleteFlag(false);
            try{
                int result=apiService.saveCreateApi(api);
                if(result>0){
                    returnObject.setCode(Contents.RETURN_CODE_SUCCESS);
                }else{
                    returnObject.setCode(Contents.RETURN_CODE_FAIL);
                    returnObject.setMessage("添加失败");
                }
            }catch (Exception e){
                e.printStackTrace();
                returnObject.setCode(Contents.RETURN_CODE_FAIL);
                returnObject.setMessage("系统出了点小问题....");
            }
        }else{
            returnObject.setCode(Contents.RETURN_CODE_FAIL);
            returnObject.setMessage("变量已存在");
        }
        return returnObject;
    }

    @GetMapping("/queryAllApiByPageForConditions")
    public Map<String, Object> queryAllApiByPageForConditions(String name,Integer pageNo,Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        map.put("name",name);
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        List<Api> list=apiService.queryAllApiByPageForConditions(map);
        int total=apiService.queryAllByPageForConditionsToTotal(map);
        map=new HashMap<>();
        map.put("list",list);
        map.put("total",total);
        return map;
    }

    @PostMapping("/saveEditApiById")
    public @ResponseBody Object saveEditApiById(@RequestBody Api api){
        api.setUpdateTime(new DateTime());
        ReturnObject returnObject=new ReturnObject();
        try {
            int result=apiService.saveEditApi(api);
            if(result>0){
                returnObject.setCode(Contents.RETURN_CODE_SUCCESS);
            }else{
                returnObject.setCode(Contents.RETURN_CODE_FAIL);
                returnObject.setMessage("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            returnObject.setCode(Contents.RETURN_CODE_FAIL);
            returnObject.setMessage("修改失败");
        }
        return returnObject;
    }

    @PostMapping("/removeApiById")
    public @ResponseBody Object removeApiById(@RequestBody Long id){
        Api api=new Api();
        api.setId(id);
        api.setDeleteFlag(true);
        api.setDeleteTime(new DateTime());
        ReturnObject returnObject=new ReturnObject();
        try {
            int result=apiService.removeApiById(api);
            if(result>0){
                returnObject.setCode(Contents.RETURN_CODE_SUCCESS);
            }else{
                returnObject.setCode(Contents.RETURN_CODE_FAIL);
                returnObject.setMessage("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            returnObject.setCode(Contents.RETURN_CODE_FAIL);
            returnObject.setMessage("系统出了点小问题....");
        }
        return returnObject;
    }

    @PostMapping("/queryApiById")
    public Api queryApiById(Long id){
        return apiService.queryApiById(id);
    }

}
